//
//  ZGLChannelCollectionViewCell.m
//  Faker
//
//  Created by dllo on 15/10/31.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLChannelCollectionViewCell.h"
#import "ZGLChannelTableViewCell.h"
#import "ZGLHTTPTOOL.h"
#import "ZGLChannelModel.h"
#import "ZGLSubTableViewCell.h"
#import "ZGLOptionsModel.h"

@interface ZGLChannelCollectionViewCell () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSUserDefaults *user;
@property (nonatomic, strong) NSMutableArray *localArr;
@property (nonatomic, strong) NSMutableArray *weixinArr;

@property (nonatomic, strong) NSMutableDictionary *signDic;
@property (nonatomic, strong) NSMutableArray *signArr;
@property (nonatomic, strong) UITableView *channelTableView;
@property (nonatomic, strong) NSMutableArray *subArr;

@property (nonatomic, strong) NSMutableArray *subOptionsArr;
@property (nonatomic, strong) NSMutableDictionary *dic;

@property (nonatomic, strong) NSMutableArray *titleArr;

@property (nonatomic, copy) NSString *soyStr;


@property (nonatomic, strong) NSMutableArray *life1;
@property (nonatomic, strong) NSMutableArray *life2;
@property (nonatomic, strong) NSMutableArray *life3;
@property (nonatomic, strong) NSMutableArray *life4;
@property (nonatomic, strong) NSMutableArray *life5;
@property (nonatomic, strong) NSMutableArray *life6;





@end

@implementation ZGLChannelCollectionViewCell

- (void)dealloc {
    _channelTableView.delegate = nil;
    _channelTableView.dataSource = nil;
    _subOptionsTableView.delegate = nil;
    _subOptionsTableView.dataSource = nil;
    [_button removeTarget:self action:@selector(quxiao:) forControlEvents:(UIControlEventTouchUpInside)];
}
- (void)createTableView {
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.subOptionsTableView = [[UITableView alloc] initWithFrame:CGRectMake(ScaleX * 375, 0, 0, self.frame.size.height) style:(UITableViewStylePlain)];
    [UIView animateWithDuration:0.3 animations:^{
        [_button setFrame:CGRectMake(ScaleX * 157.5, 0 , ScaleX * 30 , ScaleY * 30)];
        [self.subOptionsTableView setFrame:CGRectMake(ScaleX * 187.5, 0, ScaleX * 187.5, self.frame.size.height)];
    }];
    self.subOptionsTableView.delegate = self;
    self.subOptionsTableView.dataSource = self;
    [self.subOptionsTableView registerClass:[ZGLSubTableViewCell class] forCellReuseIdentifier:@"ZGLChannelCollectionViewCell_ZGLSubTableViewCell"];
    [self.contentView addSubview:self.subOptionsTableView];
    _isCreate = YES;
    [self.contentView addSubview:_button];
    [_button setImage:[UIImage imageNamed:@"quxiao"] forState:(UIControlStateNormal)];
    [_button addTarget:self action:@selector(quxiao:) forControlEvents:(UIControlEventTouchUpInside)];
}
- (void)quxiao:(UIButton *)button {
    [UIView animateWithDuration:0.5 animations:^{
        [_button setFrame:CGRectMake(0, 0 , 0 , ScaleY * 30)];
        [self.subOptionsTableView setFrame:CGRectMake(ScaleX * 375, 0, 0, self.frame.size.height)];
    }];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.user = [NSUserDefaults standardUserDefaults];
        _localArr = [self.user arrayForKey:@"localNews"].mutableCopy;
        _weixinArr = [self.user arrayForKey:@"weixin"].mutableCopy;
        _isCreate = NO;
        self.channelTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScaleX * 375, self.frame.size.height) style:(UITableViewStylePlain)];
        self.channelTableView.delegate = self;
        self.channelTableView.dataSource = self;
        [self.channelTableView registerClass:[ZGLChannelTableViewCell class] forCellReuseIdentifier:@"ZGLChannelCollectionViewCell_ZGLChannelTableViewCell"];
        [self.contentView addSubview:self.channelTableView];
        [self handleData];
    }
    return self;
}
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    [super applyLayoutAttributes:layoutAttributes];
    self.channelTableView.backgroundColor = foreColor;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.subOptionsTableView) {
        if (_titleArr.count == 0) {
            return 1;
        } else {
            return _titleArr.count;
        }
    } else {
        return 1;
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.subOptionsTableView) {
        if (_titleArr.count > 0) {
            if (0 == section) {
                return _life1.count;
            }
            if (1 == section) {
                return _life2.count;
            }
            if (2 == section) {
                return _life3.count;
            }
            if (3 == section) {
                return _life4.count;
            }
            if (4 == section) {
                return _life5.count;
            } else {
                return _life6.count;
            }
        } else if ([_soyStr isEqualToString:@"本地新闻"]) {
            return _localArr.count;
        } else if ([_soyStr isEqualToString:@"微信公众号"]) {
            return _weixinArr.count;
        } else {
            if (_subOptionsArr.count == 0) {
                return 5;
            } else {
                return _subOptionsArr.count;
            }
        }
    } else {
        if (self.subArr.count == 0) {
            return 5;
        } else {
            return self.subArr.count;
        }
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.subOptionsTableView) {
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        self.signArr = [user arrayForKey:@"currentArr"].mutableCopy;
        ZGLSubTableViewCell *celll = [self.subOptionsTableView cellForRowAtIndexPath:indexPath];
        for (NSDictionary *dic in _signArr) {
            if ([dic[@"title"] isEqualToString:celll.titleLabel.text]) {
                [celll.chooseButton setSelected:NO];
            }
        }
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(ScaleX * 20, 0, ScaleX * 335, ScaleY * 20)];
    
    if (tableView == self.subOptionsTableView) {
        view2.backgroundColor = backColor;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScaleX *335, ScaleY *20)];
        label.textColor = [UIColor lightGrayColor];
        label.font = [UIFont systemFontOfSize:12];
        if (self.titleArr.count == 0) {
            label.text = _soyStr;
        } else {
            label.text = self.titleArr[section];
        }
        [view2 addSubview:label];
    }
    return view2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView == self.subOptionsTableView) {
        return 20;
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.subOptionsTableView) {
        ZGLSubTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLChannelCollectionViewCell_ZGLSubTableViewCell"];
        if (_titleArr.count > 0) {
            if (indexPath.section == 0) {
                cell.subOptionsModel = _life1[indexPath.row];
            }
            else if (indexPath.section == 1) {
                cell.subOptionsModel = _life2[indexPath.row];
            }
           else if (indexPath.section == 2) {
                cell.subOptionsModel = _life3[indexPath.row];
            }
           else if (indexPath.section == 3) {
                cell.subOptionsModel = _life4[indexPath.row];
            }
           else if (indexPath.section == 4) {
                cell.subOptionsModel = _life5[indexPath.row];
            } else {
                cell.subOptionsModel = _life6[indexPath.row];
            }
        } else if ([_soyStr isEqualToString:@"本地新闻"]) {
            cell.soyDic = _localArr[indexPath.row];
        } else if ([_soyStr isEqualToString:@"微信公众号"]) {
            cell.soyDic = _weixinArr[indexPath.row];
        } else {
            cell.subOptionsModel = _subOptionsArr[indexPath.row];
        }
        return cell;
    } else {
        ZGLChannelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZGLChannelCollectionViewCell_ZGLChannelTableViewCell"];
        cell.channelModel = self.subArr[indexPath.row];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
           [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView == self.channelTableView) {
        ZGLChannelModel *soyModel = self.subArr[indexPath.row];
        _soyStr = soyModel.title;
        [UIView animateWithDuration:0.3 animations:^{
            self.subOptionsTableView.contentOffset = CGPointMake(0, 0);
        }];
        _subOptionsArr = [NSMutableArray arrayWithCapacity:0];
        self.titleArr = [NSMutableArray arrayWithCapacity:0];
        ZGLChannelModel *model = _subArr[indexPath.row];
        /* 选同一个收回 选不同的 内容改变 */
        NSMutableArray *arr = [self shareDataDic];
        if (arr.count == 0) {
            [arr addObject:model.title];
            [user setObject:arr forKey:@"current"];
        }
        NSString *str = arr[0];
        if ([str isEqualToString:model.title]) {
            if (self.subOptionsTableView.frame.size.width == 0) {
                [UIView animateWithDuration:0.3 animations:^{
                    [_button setFrame:CGRectMake(ScaleX * 157.5, 0 , ScaleX * 30 , ScaleY * 30)];
                    [self.subOptionsTableView setFrame:CGRectMake(ScaleX * 187.5, 0, ScaleX * 187.5, self.frame.size.height)];
                }];
            } else {
                [UIView animateWithDuration:0.3 animations:^{
                    [_button setFrame:CGRectMake(0, 0 , 0 , ScaleY * 30)];
                    [self.subOptionsTableView setFrame:CGRectMake(ScaleX * 375, 0, 0, self.frame.size.height)];
                }];
            }
        } else {
            if (self.subOptionsTableView.frame.size.width == 0) {
                [UIView animateWithDuration:0.3 animations:^{
                    [_button setFrame:CGRectMake(ScaleX * 157.5, 0 , ScaleX * 30 , ScaleY * 30)];
                    [self.subOptionsTableView setFrame:CGRectMake(ScaleX * 187.5, 0, ScaleX * 187.5, self.frame.size.height)];
                }];
              
            }
            arr = [NSMutableArray arrayWithCapacity:0];
            [arr addObject:model.title];
            [user setObject:arr forKey:@"current"];
        }
        if ([model.title isEqualToString:@"生活"]) {
            self.titleArr = [NSMutableArray arrayWithCapacity:0];
            self.life1 = [NSMutableArray arrayWithCapacity:0];
            self.life2 = [NSMutableArray arrayWithCapacity:0];
            self.life3 = [NSMutableArray arrayWithCapacity:0];
            self.life4 = [NSMutableArray arrayWithCapacity:0];
            self.life5 = [NSMutableArray arrayWithCapacity:0];
            self.life6 = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary *dic in model.sons) {
                if ([dic[@"title"] isEqualToString:@"生活精选"]) {
                    for (NSDictionary *dict in dic[@"sons"]) {
                        ZGLOptionsModel *model1 = [ZGLOptionsModel modWithDic:dict];
                        [_life1 addObject:model1];
                    }
                }
                if ([dic[@"title"] isEqualToString:@"生活百科"]) {
                    for (NSDictionary *dict in dic[@"sons"]) {
                    
                        ZGLOptionsModel *model1 = [ZGLOptionsModel modWithDic:dict];
                        [_life2 addObject:model1];
                    }
                }
                if ([dic[@"title"] isEqualToString:@"健康养生"]) {
                    for (NSDictionary *dict in dic[@"sons"]) {
                   
                        ZGLOptionsModel *model1 = [ZGLOptionsModel modWithDic:dict];
                        [_life3 addObject:model1];
                    }
                }
                if ([dic[@"title"] isEqualToString:@"亲子育儿"]) {
                    for (NSDictionary *dict in dic[@"sons"]) {
                  
                        ZGLOptionsModel *model1 = [ZGLOptionsModel modWithDic:dict];
                        [_life4 addObject:model1];
                    }
                }
                if ([dic[@"title"] isEqualToString:@"奇趣发现"]) {
                    for (NSDictionary *dict in dic[@"sons"]) {
                        ZGLOptionsModel *model1 = [ZGLOptionsModel modWithDic:dict];
                        [_life5 addObject:model1];
                    }
                }
                if ([dic[@"title"] isEqualToString:@"心灵情感"]) {
                    for (NSDictionary *dict in dic[@"sons"]) {
                        ZGLOptionsModel *model1 = [ZGLOptionsModel modWithDic:dict];
                        [_life6 addObject:model1];
                    }
                }
                [self.titleArr addObject:dic[@"title"]];
            }
        } else {
            for (NSDictionary *dic in model.sons) {
                ZGLOptionsModel *model1 = [ZGLOptionsModel modWithDic:dic];
                [_subOptionsArr addObject:model1];
            }
        }
        if (_isCreate == NO) {
            [self createTableView];
        }
        [self.subOptionsTableView reloadData];
    }
    if (tableView == self.subOptionsTableView) {
        ZGLOptionsModel *model = nil;
        if (_titleArr.count > 0) {
            if (indexPath.section == 0) {
                model = _life1[indexPath.row];
            }
            else if (indexPath.section == 1) {
                model = _life2[indexPath.row];
            }
            else if (indexPath.section == 2) {
                model = _life3[indexPath.row];
            }
            else if (indexPath.section == 3) {
                model = _life4[indexPath.row];
            }
            else if (indexPath.section == 4) {
                model = _life5[indexPath.row];
            } else {
                model = _life6[indexPath.row];
            }
        } else if ([_soyStr isEqualToString:@"本地新闻"]) {
            model = [ZGLOptionsModel modWithDic:_localArr[indexPath.row]];
        } else if ([_soyStr isEqualToString:@"微信公众号"]) {
            model = [ZGLOptionsModel modWithDic:_weixinArr[indexPath.row]];
        }
        else {
            model = _subOptionsArr[indexPath.row];
        }
        [self.delegate passDic:model];
    }
}


- (NSMutableArray *)shareDataDic {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSMutableArray *arr = [user arrayForKey:@"current"].mutableCopy;
    if (arr.count == 0) {
        [user setObject:[NSMutableArray arrayWithCapacity:0] forKey:@"current"];
    }
    arr = [user arrayForKey:@"current"].mutableCopy;
    return arr;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.subOptionsTableView) {
        return ScaleY * 60;
    } else {
        return ScaleY * 60;
    }
}

- (void)handleData {
    [ZGLHTTPTOOL GETWithURL:@"http://iphone.myzaker.com/zaker/apps_v3.php?_appid=AndroidPhone&_version=6.41&m=1445400365" withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        self.subArr = [NSMutableArray arrayWithCapacity:0];
        NSArray *arr = result[@"data"][@"datas"];
        for (NSDictionary *dic in arr) {
            ZGLChannelModel *model = [ZGLChannelModel modWithDic:dic];
            [self.subArr addObject:model];
        }
        [self.channelTableView reloadData];
    } withFail:^(id result) {
        
    }];
}


@end
